package cn.edu.zafu.bigdata.x.zbw;

import java.io.*;
import java.util.*;

public class StudentManagementSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            String input = scanner.nextLine();
            switch (input) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    searchStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    deleteStudent();
                    break;
                case "5":
                    System.out.println("程序退出。");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的输入，请重新输入。");
            }
        }
    }

    public static void addStudent() {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.dat", true));
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入学号: ");
            String id = scanner.nextLine();
            while (searchStudent(id) != null) {
                System.out.println("学号重复，请重新输入学号: ");
                id = scanner.nextLine();
            }
            System.out.print("请输入姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入电话: ");
            String phone = scanner.nextLine();
            Student student = new Student(id, name, phone);
            oos.writeObject(student);
            oos.close();
            System.out.println("学生信息添加成功。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Student searchStudent(String id) {
        try {
            FileInputStream fis = new FileInputStream("students.dat");
            ObjectInputStream ois = new ObjectInputStream(fis);
            while (true) {
                Student student = (Student) ois.readObject();
                if (student == null) break;
                if (student.getId().equals(id)) {
                    ois.close();
                    return student;
                }
            }
            ois.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void searchStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号进行检索: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = searchStudent(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    public static void updateStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号进行更新: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = searchStudent(id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                saveStudent(student);
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    public static void deleteStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号进行删除: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = searchStudent(id);
            if (student != null) {
                ArrayList<Student> students = new ArrayList<>();
                try {
                    FileInputStream fis = new FileInputStream("students.dat");
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    while (true) {
                        Student s = (Student) ois.readObject();
                        if (s == null) break;
                        if (!s.getId().equals(id)) {
                            students.add(s);
                        }
                    }
                    ois.close();
                    fis.close();
                    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.dat"));
                    for (Student s : students) {
                        oos.writeObject(s);
                    }
                    oos.writeObject(null);
                    oos.close();
                    System.out.println("学生信息删除成功。");
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    private static void saveStudent(Student student) {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.dat"));
            FileInputStream fis = new FileInputStream("students.dat");
            ObjectInputStream ois = new ObjectInputStream(fis);
            ArrayList<Student> students = new ArrayList<>();
            while (true) {
                Student s = (Student) ois.readObject();
                if (s == null) break;
                students.add(s);
            }
            ois.close();
            fis.close();
            for (Student s : students) {
                if (s.getId().equals(student.getId())) {
                    oos.writeObject(student);
                } else {
                    oos.writeObject(s);
                }
            }
            oos.writeObject(null);
            oos.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;
    private String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}
